package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Space;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.fragment.CommonDialog;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView textView;

    @BindView(R.id.img_atar)
    ImageView img_atar;

    @BindView(R.id.tv_phone)
    TextView tv_phone;

    @BindView(R.id.tv_card)
    TextView tv_card;

    @BindView(R.id.tv_real_name)
    TextView tv_real_name;

    @BindView(R.id.tv_wxname)
    TextView tv_wxname;

    UserPresenter userPresenter;

    @Override
    protected int layoutResID() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initData() {
        textView.setText("个人中心");
        userPresenter=new UserPresenter(this);
        getUserDetail();
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (actionType.equals(ConstantValue.RequestKey.userinfo)) {
            UserDetailVo userDetailVo = (UserDetailVo) data;
            if (userDetailVo != null) {


                if(userDetailVo.getAvatar()==null||userDetailVo.getAvatar().length()==0){
                    ImageLoader.displayImageDefaultCircle(context,R.mipmap.logo, img_atar);
                }else {
                    ImageLoader.displayImageDefaultCircle(context, userDetailVo.getAvatar(), img_atar);
                }
                tv_phone.setText(userDetailVo.getCellphone()==null?"":userDetailVo.getCellphone());
                tv_card.setText(userDetailVo.getIdcardCode()==null?"":userDetailVo.getIdcardCode());
                tv_real_name.setText(userDetailVo.getRealName()==null?"":userDetailVo.getRealName());
                tv_wxname.setText(userDetailVo.getNick()==null?"未绑定":userDetailVo.getNick());
            }
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @OnClick({R.id.img_back,R.id.rl_pay_setting,R.id.ll_log_out,R.id.rl_v_code})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_pay_setting:
                intent =new Intent(context,PaySettingActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_log_out:
                loginOut();
                break;
            case R.id.rl_v_code:
                intent=new Intent(context,AboutUsActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        CommonDialog loginOutDialog = CommonDialog.getInstance();
        loginOutDialog.show(getSupportFragmentManager(), "loginout");
        loginOutDialog.setClickCallback(new CommonDialog.OnClickCallback() {
            @Override
            public void cancel() {
                loginOutDialog.dismiss();
            }

            @Override
            public void confirm() {
                SPUtils.remove(context, ConstantValue.SpType.token);
                SPUtils.remove(context, ConstantValue.SpType.newtoken);
                SPUtils.remove(context, ConstantValue.SpType.phone);
                SPUtils.remove(context, ConstantValue.SpType.userid);
                SPUtils.remove(context,ConstantValue.SpType.isreal);
                ToastUtils.showShortToast("已经退出当前用户");
                loginOutDialog.dismiss();
                MainActivity.mActivity.finish();
                Intent intent =new Intent(context,LoginActivityNew.class);
                startActivity(intent);
                UserInfoActivity.this.finish();
            }
        });
    }

    public void getUserDetail() {
        userPresenter.getUserDetail(ConstantValue.RequestKey.userinfo);
    }
}
